﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Model;
using System.Web.Security;
using System.IO;
using System.Collections.ObjectModel;
using University.Workshop.Business;
using University.Workshop.DataAccess;

namespace University.Workshop.WebApp
{
    public partial class MyOffers : System.Web.UI.Page
    {
        Product selectedProduct = new Product();
        User user = new User();
        Offer offer= new Offer();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!User.Identity.IsAuthenticated)
                    FormsAuthentication.RedirectToLoginPage();
                user = UserDataAccess.GetUserByEmail(User.Identity.Name);
                long productId = long.Parse(Request["idProduct"]);
                selectedProduct = DataAccess.ProductDataAccess.GetProductById(productId);
                ProductName.Text = selectedProduct.Name;
                ProductPrice.Text = selectedProduct.Price.ToString();
                if (Directory.Exists(Server.MapPath("~/Thumbnails/") + selectedProduct.ProductId + "/"))
                {
                    DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + selectedProduct.ProductId + "/");
                    FileInfo[] currentImages = dir.GetFiles();
                    string thumbUrl = "~/Thumbnails/" + selectedProduct.ProductId + "/";
                    foreach (FileInfo fi in currentImages)
                    {
                        if (String.IsNullOrEmpty(Image1.ImageUrl))
                        {
                            Image1.ImageUrl = thumbUrl + fi.Name;

                        }
                    }
                }
                else
                {
                    Image1.ImageUrl = "~/Thumbnails/NoImage/noimage.jpg";
                }
                Collection<Offer> offerList = new Collection<Offer>();
                offerList = OfferManager.GetOffers(selectedProduct.ProductId);
                if (offerList != null)
                {
                    dlWatchList.DataSource = offerList;
                    dlWatchList.DataBind();
                }
                else Label1.Visible = true;
            }

        }

        protected void Accept_Click1(object sender, EventArgs e)
        {
            try
            {
                    Button b = sender as Button;
                    offer = OfferManager.GetOneOffer(Convert.ToInt64(b.CommandArgument), long.Parse(Request["idProduct"]));
                    offer=OfferManager.ModifyOffer(offer, "-1", "Accepted",true);
                    Response.Redirect("UserCP.aspx");
            }
            catch (Exception ex)
            {
                Errorlb.Text = ex.Message;
                Errorlb.Visible = true;
            }
        }

        protected void Decline_Click1(object sender, EventArgs e)
        {
            try
            {
                    Button b = sender as Button;
                    offer = OfferManager.GetOneOffer(Convert.ToInt64(b.CommandArgument), long.Parse(Request["idProduct"]));
                    Session["offer"] = offer;
                    Response.Redirect("Decline.aspx");
            }
            catch (Exception ex)
            {

                Errorlb.Text = ex.Message;
            }

        }

        protected void dlWatchList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}